﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;

namespace Medlcalcare.MVC.Common
{
    /// <summary>
    /// 自定义业务异常类
    /// 用于封装医疗AI相关异常
    /// </summary>
    public class MedicalAIException : Exception
    {
        /// <summary>
        /// 异常严重级别
        /// </summary>
        public SeverityLevel Severity { get; }

        public MedicalAIException(string message, SeverityLevel severity = SeverityLevel.Normal)
            : base(message)
        {
            Severity = severity;
        }

        public MedicalAIException(string message, Exception innerException,
            SeverityLevel severity = SeverityLevel.Critical)
            : base(message, innerException)
        {
            Severity = severity;
        }

        public enum SeverityLevel
        {
            Normal,
            High,
            Critical
        }
    }

    // 全局异常过滤器
    public class GlobalExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            if (context.Exception is MedicalAIException ex)
            {
                // 记录医疗业务异常
                context.Result = new ViewResult
                {
                    ViewName = "MedicalError",
                    StatusCode = (int)HttpStatusCode.InternalServerError
                };
                context.ExceptionHandled = true;
            }
        }
    }
}
